package com.tgz.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.tgz.common.R;
import com.tgz.config.MyVar;
import com.tgz.constant.Phone;
import com.tgz.model.Bscreen;
import com.tgz.service.IBscreenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

/**
 * <p>
 * 弹幕 前端控制器
 * </p>
 *
 * @author hsj
 * @since 2022-12-22
 */
@RestController
@RequestMapping("/bscreen")
public class BscreenController {

    @Resource
    private IBscreenService iBscreenService;
    @Autowired
    public SimpMessagingTemplate template;
    @Autowired
    private MyVar myVar;

    @ResponseBody
    @RequestMapping(value = "/allList",method = RequestMethod.POST)
    public Object allList() throws Exception {
        return R.responseWithCodeStatusMessageDataEnhance(iBscreenService.findAllBscreeWithNoCheckStatus());
    }

    /**
     * @Description:签到添加手机号
     * @author HeShengjin 2356899074@qq.com
     * @date 2022/12/22 14:33
     */
    @ResponseBody
    @RequestMapping(value = "/addMsg",method = RequestMethod.POST)
    public Object addMsg(@RequestParam String msg) throws Exception {
        Integer addMsg = iBscreenService.addMsg(msg);
        return R.responseWithCodeStatusMessageDataEnhance(addMsg);
    }

    /**
     * @Description:删除id
     * @author HeShengjin 2356899074@qq.com
     * @date 2022/12/22 15:13
     */
    @ResponseBody
    @RequestMapping(value = "/deleteById",method = RequestMethod.POST)
    public Object deleteById(@RequestParam Long id) throws Exception {
        boolean update = iBscreenService.update(new Bscreen() {{
                                                  setIsDeleted(com.tgz.constant.Phone.IS_DELETED_1);
                                                  setStatus(com.tgz.constant.Phone.STATUS_0);
                                              }},
                new LambdaUpdateWrapper<Bscreen>().eq(Bscreen::getId, id));
        //删除client弹幕
        if(update){
            Bscreen bscreenEntity = iBscreenService.getById(id);
            bscreenEntity.setIdStr(bscreenEntity.getId().toString());
            template.convertAndSend(myVar.getTopicChannel(), bscreenEntity);
        }
        return R.responseWithCodeStatusMessageDataEnhance(update ? 1 : 0);
    }

    /**
     * @Description:过审id
     * @author HeShengjin 2356899074@qq.com
     * @date 2022/12/22 15:13
     */
    @ResponseBody
    @RequestMapping(value = "/csById",method = RequestMethod.POST)
    public Object csById(@RequestParam Long id) throws Exception {
        boolean update = iBscreenService.update(new Bscreen() {{
                                                    setStatus(com.tgz.constant.Phone.STATUS_1);
                                                }},
                new LambdaUpdateWrapper<Bscreen>().eq(Bscreen::getId, id));
        //过审，发送client弹幕
        if(update){
            Bscreen bscreenEntity = iBscreenService.getById(id);
            bscreenEntity.setIdStr(bscreenEntity.getId().toString());
            template.convertAndSend(myVar.getTopicChannel(), bscreenEntity);
        }
        return R.responseWithCodeStatusMessageDataEnhance(update ? 1 : 0);
    }

    /**
     * @Description:下载数据
     * @author HeShengjin 2356899074@qq.com
     * @date 2022/12/22 15:32
     */
    @GetMapping("/downloadExcel")
    public void download(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("云南特盖则科技有限公司2022年会在线签到-弹幕数据", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), Bscreen.class).sheet("模板").doWrite(iBscreenService.findAllBscree());
    }
}
